var app = require("express")();
var mysql = require("mysql");

//连接数据库
var connection = mysql.createConnection({
   host:"localhost",
   user:"root",
    password:"",
    database:"Friday"
});
connection.connect();//链接

app.get("/index",function (req,res) {
    //获取前端提交的内容
    //sql语句
    var query = "SELECT * FROM first";

    //执行sql语句
    connection.query(query,function (err,result) {
        if(err){
            console.log(err);
            res.send('{"err":0,"msg":'+err+'}');
        }else{
        	res.send(result);
        }
    })
});

//注册
app.get('/zhuce',function(req,res){
	//获取前端传过来的值
	var userObj= req.query;
	var userName=userObj.username;
	var passWord= userObj.password;
	var sql1 = "select * from zhuce where username = "+userName
	connection.query(sql1,function (err,result){
		if(result.length==0){
			//sql语句
				var sql = "INSERT INTO zhuce(id,username,password) VALUES(NULL,'"+userName+"','"+passWord+"')";
				connection.query(sql,function (err,result) {
			        if(err){
			            res.send('{"err":0,"msg":'+err+'}');
			        }else{
			        		console.log(result)
			            var id = result.insertId;
			            res.send('{"err":1,"id":'+id+'}');
			        }
			    })
			}else{
			res.send('{"err":2}')
		}
	})
})
//登陆
app.get('/login',function(req,res){
	//获取传来的值
	var userObj= req.query;
	var userName = userObj.username;
	var passWord= userObj.password;
	var sql = "select * from zhuce where username = "+userName;
	connection.query(sql,function (err,result){
		if(err){
			res.send('{"err":2}')
		}else{
			if(result[0].password == passWord){
				res.send('{"err":1}')
			}else{
				res.send('{"err":0}')
			}
		}
	})
})
//详情页
app.get('/detail',function(req,res){
	var Obj =req.query;
	var Id = Obj.id;
	var sql = "select * from first where id = "+Id
	connection.query(sql,function(err,result){
		if(err){
			res.send('{"err":2,"msg":"错误"}')
		}else{
			res.send(result)
		}
	})
})

//加入购物车
app.get('/joinCar',function(req,res){
	var Obj =req.query;
	var Id = Obj.id;
	var sql = "select * from first where id = "+Id
	connection.query(sql,function(err,result){
		if(err){
			res.send('{"err":2,"msg":"错误"}')
		}else{
			var Img =  result[0].picture;
			console.log(Img)
			var Price = result[0].price;
			var Cont = req.query.cont;
			var sum = Cont*Price;
			var name = result[0].name;
			var sql1 = "INSERT INTO shopcar (id,img,price,cont,sum,name) VALUES(NULL,'"+Img+"','"+Price+"','"+Cont+"','"+sum+"','"+name+"')"
			connection.query(sql1,function(err,result){
				if(err){
					res.send('{"err":2,"msg":"错误"}')
				}else{
					var id = result.insertId;
					res.send('{"err":1,"id":'+id+'}')
				}
			})
		}
	})
})

//购物车
app.get('/myshopcar',function(req,res){
	var sql = "SELECT * FROM shopcar"
	connection.query(sql,function (err,result) {
        if(err){
            console.log(err);
            res.send('{"err":0,"msg":'+err+'}');
        }else{
        	console.log(result)
        	res.send(result);
        }
    })
})

//购物车删除
app.get('/remove',function(req,res){
	var Id = req.query.id
	console.log(Id)
	var sql = "delete from shopcar where id ="+Id
	connection.query(sql,function(err,result){
		if(err){
			res.send('{"err":0,"msg":'+err+'}');
		}else{
			res.send('{"err":1}')
		}
	})
})

//添加地址
app.get('/address',function(req,res){
	var Name = req.query.name
	var Phone = req.query.phone
	var Site = req.query.site
	var sql = "INSERT INTO address (id,name,phone,site) VALUES(NULL,'"+Name+"','"+Phone+"','"+Site+"')"
	connection.query(sql,function(err,result){
		if(err){
			res.send('{"err":0,"msg":'+err+'}')
		}else{
			res.send('{"err":1}')
		}
	})
})

//获得地址
app.get('/getaddress',function(req,res){
	var sql = "SELECT * FROM address"
	connection.query(sql,function(err,result){
		if(err){
			res.send('{"err":0,"msg":'+err+'}')
		}else{
			res.send(result)
		}
	})
})

//删除地址
app.get('/removeaddress',function(req,res){
	var Id = req.query.id
	var sql = "delete from address where id ="+Id
	connection.query(sql,function(err,result){
		if(err){
			res.send('{"err":0,"msg":'+err+'}')
		}else{
			res.send('{"err":1}')
		}
	})
})


//获取我的消息
app.get('/mynews',function(req,res){
	//sql语句
    var query = "SELECT * FROM mynews";
     //执行sql语句
    connection.query(query,function (err,result) {
        if(err){
            console.log(err);
            res.send('{"err":0,"msg":'+err+'}');
        }else{
          	res.send(result);
        }
    })
    
    
})
//删除消息
app.get('/news',function(req,res){
	//获取前端传来的值
    var _id = req.query.id;
    //sql语句(按消息id查询消息)
    var sql1 = "delete from mynews where id =" + _id;
    connection.query(sql1,function(err,result){
    	   if(!err){
    	   	res.send('{"err":1}')
    	   }else{
    	   	res.send('{"err":0,"msg":'+err+'}')
    	   }
    })
})
//我的账户
app.get("/account",function(req,res){
	//接收前端传来的值
	var acc = req.query.account;
	//sql语句
	var sql ="select * from account where accounts = "+acc;
	//执行sql语句
	connection.query(sql,function(err,result){
		if(err){
			res.send('{"err":0}')
		}else{
			res.send(result)
		}
	})
})

//服务器路径
app.get("*",function (req,res) {
    res.sendfile("."+req.url);
})

app.listen(8000,function (err) {
    console.log("服务器启动成功!")
})